import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { addressType } from '@/types/productDetai'
export const useAddressStore = defineStore(
  'address',
  () => {
    const address = ref<addressType>({} as addressType)
    const setAddresInfo = (addressObj: addressType) => {
      address.value!.province = addressObj.province
      address.value!.city = addressObj.city
      address.value!.district = addressObj.district
      address.value!.name = addressObj.name
      address.value!.telephone = addressObj.telephone
      address.value!.address = addressObj.address
      address.value!.id = addressObj.id
    }
    return { address, setAddresInfo }
  },
  // TODO: 持久化
  {
    persist: {
      // 注意：默认持久化，只在H5端有效，因为默认使用的是localStorage，小程序端不能使用这个API，所以需要重写存取方法，转调给UniApp的持久化方法
      // persist: true,
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, val) {
          uni.setStorageSync(key, val)
        },
      },
    },
  },
)
